package org.example.covid.partion;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.example.covid.entity.CovidPartitionEntity;


import java.io.IOException;

public class CovidPartitionApp {

    public static class MyMapper extends Mapper<LongWritable, Text, CovidPartitionEntity, Text> {

        private CovidPartitionEntity outKey = new CovidPartitionEntity();
        /**
         * 每对k,v执行一次
         * input: <LongWritable, Text>
         * output: <CovidPartitionEntity,Text>
         * @param key
         * @param value
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //读取一行，进行切割
            String[] fileds = value.toString().split(",");
            //state
            outKey.set(fileds[0],fileds[1],fileds[2]);
            //输出结果
            context.write(outKey,value);
        }
    }

    public static class MyReducer extends Reducer<CovidPartitionEntity, Text, Text, NullWritable> {

        @Override
        protected void reduce(CovidPartitionEntity key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text value : values) {
                context.write(value,NullWritable.get());
            }
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        if (args.length < 2) {
            System.err.println("Usage: partition <in> <out>");
            System.exit(2);
        }
        //1、获取job
        //创建Hadoop配置对象，使用默认配置
        Configuration conf = new Configuration();
//        conf.set("mapreduce.framework.name","local");
        //创建一个MR作业，取名叫做word count，名字可用于管理作业
        Job job = Job.getInstance(conf, CovidPartitionApp.class.getSimpleName());

        //2、设置jar包路径
        //告诉job可以跟据App这个类的元数据信息找到MR的jar文件
        job.setJarByClass(CovidPartitionApp.class);

        //3、关联mapper和reducer
        //将Mapper类加入job
        job.setMapperClass(MyMapper.class);
        //将Reducer类设置为合并器，在Mapper端执行Reducer的逻辑，减少输出数据
//        job.setCombinerClass(MyReducer.class);
        //将Reducer类加入job
        job.setReducerClass(MyReducer.class);

        //4、设置最终输出的k,v类型
        //设置mapper阶段输出的key value 类型
        job.setMapOutputKeyClass(CovidPartitionEntity.class);
        job.setMapOutputValueClass(Text.class);
        //设置MR最终输出的数据的Key类型,Value类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);


        //todo 设置程序partition 规则,设置Reducer数量
        job.setNumReduceTasks(6);
        job.setPartitionerClass(StatePartitioner.class);


        //6、设置输入输出路径
        //设置输入数据从哪个目录正的文件中读
        FileInputFormat.addInputPath(job, new Path(args[0]));
        //设置输出数据存放的文件在哪个目录下
        Path output = new Path(args[1]);
        FileOutputFormat.setOutputPath(job, output);

        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(output)) {
            fs.delete(output,true);
        }

        //7、提交job
        //执行job并等待其结束，然后程序退出
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
